package com.holy.sh.module_base_mvp.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 时间转换工具类
 *
 * @author yoyo
 */
public class TimeUtil {

    public static String timedate(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;

    }


    public static String getDate(){
        SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("yyyy-MM-dd HH:mm:ss");
        Date curDate =  new Date(System.currentTimeMillis());
        String   str   =   formatter.format(curDate);
        return str;
    }
    /**
     * String 转 data
     * */

    public static Date stringToDate(String time){
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
             date=sdr.parse(time);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
      return date;
    }
    /**
     * 将时间戳转成日期字符串
     * @param timeStamp 时间戳的值,类型为：Long
     *
     * @return
     */
    public static String getDateStringByTimeSTamp(Long timeStamp){
        String result = null;
        Date date = new Date(timeStamp*1000);
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        result = sd.format(date);
        return result;
    }


    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static String getTime(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date(time));
    }

    /**
     * yyyy.MM.dd
     */
    public static String getDate(long time, int type) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
        return format.format(new Date(time));
    }


    /**
     * yyyy-MM-dd
     */
    public static String getDate(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(new Date(time));
    }

    /**
     * HH:mm
     */
    public static String getMonthAndDay(long time) {
        SimpleDateFormat format = new SimpleDateFormat("MM-dd");
        return format.format(new Date(time));
    }

    /**
     * 时间段转化成 HH:mm:ss
     */
    public static String toHMS(long time) {
        long hours = time / (1000 * 60 * 60);
        long min = (time % (1000 * 60 * 60)) / (1000 * 60);
        long scond = ((time % (1000 * 60 * 60)) % (1000 * 60)) / 1000;
        //SimpleDateFormat format = new SimpleDateFormat("MM:dd:ss");
        return String.format("%02d", hours) + ":" + String.format("%02d", min) + ":" + scond;
    }

    /**
     * 星期几
     */
    public static String getWeek(long time) {
        String result = "星期";
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        switch (c.get(Calendar.DAY_OF_WEEK)) {
            case 1:
                result += "天";
                break;
            case 2:
                result += "一";
                break;
            case 3:
                result += "二";
                break;
            case 4:
                result += "三";
                break;
            case 5:
                result += "四";
                break;
            case 6:
                result += "五";
                break;
            case 7:
                result += "六";
                break;
        }

        return result;
    }

    /**
     * MM月dd日
     */
    public static String getMonthDay(long time) {
        SimpleDateFormat format = new SimpleDateFormat("MM月dd日");
        return format.format(new Date(time));
    }

    /**
     * HH:mm
     */
    public static String getHourMin(long time) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return format.format(new Date(time));
    }

/*    *//**
     * 将中国格式的时间转化为毫秒
     *
     * @param fString
     * @return
     *//*
    public static long getTime(String fString) throws ParseException {
        long mTime = 0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy" + "-" + "MM" + "-" + "dd", Locale.CHINA);
        mTime = format.parse(fString).getTime();
        return mTime;
    }*/

    /**
     * 将中国格式的时间转化为毫秒
     */
    public static long getTime(String fString) {
        long mTime = 0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        try {
            mTime = format.parse(fString).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return mTime;
    }


    /**
     * 换算新闻时间
     *
     * @param timesamp 时间戳
     * @return 换算后显示的时间
     */
    public static String getNewsTime(long timesamp) {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        Date today = new Date(System.currentTimeMillis());
        Date otherDay = new Date(timesamp);
        long temp = today.getTime() - otherDay.getTime();

        int excursionMin = (int) (temp / (1000 * 60));

        if (excursionMin <= 5 * 60) {
            result = "刚刚更新";
        } else if (excursionMin > (5 * 60) && excursionMin <= (10 * 60)) {
            result = "5小时前";
        } else if (excursionMin > (10 * 60) && excursionMin <= (24 * 60)) {
            result = "10小时前";
        } else if (excursionMin > (24 * 60)) {
            result = "1天前";
        }


        return result;
    }


    /**
     * 换算消息时间
     *
     * @param timesamp 时间戳
     * @return 换算后显示的时间
     */
    public static String getMsgTime(long timesamp) {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        Date today = new Date(System.currentTimeMillis());
        Date otherDay = new Date(timesamp);
        int temp = Integer.parseInt(sdf.format(today))
                - Integer.parseInt(sdf.format(otherDay));

        switch (temp) {
            case 0:
                result = getHourMin(timesamp);
                break;
            case 1:
                result = "昨天 " + getHourMin(timesamp);
                break;
            case 2:
                result = "前天 " + getHourMin(timesamp);
                break;

            default:
                result = getMonthDay(timesamp);
                break;
        }
        return result;
    }


    /**
     * Unix 时间戳转换
     */
    public static String getUnixTime(long time) {
        String date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date(time * 1000));
        return date;
    }

    /**
     * 获取某天某个时间点的时间戳
     *
     * @param day  距离当前几天
     * @param hour 时间
     */
    public static long getHourOfDay(long time, int day, int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time));
        calendar.add(Calendar.DAY_OF_YEAR, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        return calendar.getTime().getTime();
    }

    public static Calendar getCalender(String time){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(getTime(time)));
        return  calendar;
    }

    /**
     * 比较是不是之前
     */
    public static boolean isBefore(long time1, long time2) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time1));

        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(new Date(time2));

        if (calendar.before(calendar1)) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 判断时间是否在时间段内
     *
     * @param date         当前时间 yyyy-MM-dd HH:mm:ss
     * @param strDateBegin 开始时间 09:00:00
     * @param strDateEnd   结束时间 00:05:00
     */
    public static boolean isInDate(Date date, String strDateBegin,
                                   String strDateEnd) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = sdf.format(date);
        // 截取当前时间时分秒
        int strDateH = Integer.parseInt(strDate.substring(11, 13));
        int strDateM = Integer.parseInt(strDate.substring(14, 16));
        int strDateS = Integer.parseInt(strDate.substring(17, 19));
        // 截取开始时间时分秒
        int strDateBeginH = Integer.parseInt(strDateBegin.substring(0, 2));
        int strDateBeginM = Integer.parseInt(strDateBegin.substring(3, 5));
        int strDateBeginS = Integer.parseInt(strDateBegin.substring(6, 8));
        // 截取结束时间时分秒
        int strDateEndH = Integer.parseInt(strDateEnd.substring(0, 2));
        int strDateEndM = Integer.parseInt(strDateEnd.substring(3, 5));
        int strDateEndS = Integer.parseInt(strDateEnd.substring(6, 8));
        if ((strDateH >= strDateBeginH && strDateH <= strDateEndH)) {
            // 当前时间小时数在开始时间和结束时间小时数之间
            if (strDateH > strDateBeginH && strDateH < strDateEndH) {
                return true;
                // 当前时间小时数等于开始时间小时数，分钟数在开始和结束之间
            } else if (strDateH == strDateBeginH && strDateM >= strDateBeginM
                    && strDateM <= strDateEndM) {
                return true;
                // 当前时间小时数等于开始时间小时数，分钟数等于开始时间分钟数，秒数在开始和结束之间
            } else if (strDateH == strDateBeginH && strDateM == strDateBeginM
                    && strDateS >= strDateBeginS && strDateS <= strDateEndS) {
                return true;
            }
            // 当前时间小时数大等于开始时间小时数，等于结束时间小时数，分钟数小等于结束时间分钟数
            else if (strDateH >= strDateBeginH && strDateH == strDateEndH
                    && strDateM <= strDateEndM) {
                return true;
                // 当前时间小时数大等于开始时间小时数，等于结束时间小时数，分钟数等于结束时间分钟数，秒数小等于结束时间秒数
            } else if (strDateH >= strDateBeginH && strDateH == strDateEndH
                    && strDateM == strDateEndM && strDateS <= strDateEndS) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    public static String dateToString(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = sdf.format(date);
        return strDate;
    }


    /**
     * 两个时间内有多少毫秒
     * */
    public static long daysBetween(String startTime, String endTime)  {
       Date startDate= TimeUtil.stringToDate(startTime);
       Date endDate= TimeUtil.stringToDate(endTime);
        long betweenDays=0;
        if (startDate!=null&&endDate!=null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                startDate = sdf.parse(sdf.format(startDate));
                endDate = sdf.parse(sdf.format(endDate));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            long time1 = calendar.getTimeInMillis();


            calendar.setTime(endDate);
            long time2 = calendar.getTimeInMillis();

            betweenDays= (time2 - time1) ;
           // betweenDays= (time2 - time1) / (1000 * 3600 * 24);
            return Long.parseLong(String.valueOf(betweenDays));
        }

     return betweenDays;

    }


    public static int stringDaysBetween(String smdate, String bdate)
            throws ParseException, ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);


        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 包含当前日期12个月之后
     * @return
     */
    public static String getCurrent12MonthAfter(String dataTime) {
//        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault( ));
//        String currentTime = sdf.format(date);
        try {
            Date data = sdf.parse(dataTime);
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(data);
            if (Calendar.MONTH - 12 != 0) {
                rightNow.add(Calendar.YEAR, 0);// 年份不变
                rightNow.add(Calendar.MONTH, +1);
                Date dataNow = rightNow.getTime();
                String nowCurrentTime = sdf.format(dataNow);
                return nowCurrentTime;
            } else {
                rightNow.add(Calendar.YEAR, +1);//  年份加1
                rightNow.add(Calendar.MONTH, 1);
                Date dataNow = rightNow.getTime();
                String nowCurrentTime = sdf.format(dataNow);
                return nowCurrentTime;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }


}
